package com.dcrzl.gazgmh.module.mh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.dcrzl.gazgmh.common.entity.mh.MhBanner;
import com.dcrzl.gazgmh.common.entity.mh.MhModule;
import com.dcrzl.gazgmh.common.mapper.mh.MhBannerMapper;
import com.dcrzl.gazgmh.common.service.impl.BaseServiceImpl;
import com.dcrzl.gazgmh.common.utils.PageInfo;
import com.dcrzl.gazgmh.common.utils.Result;
import com.dcrzl.gazgmh.common.utils.SysConstant;
import com.dcrzl.gazgmh.module.mh.service.MhBannerService;
import com.dcrzl.gazgmh.module.mh.service.MhModuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.io.Serializable;
import java.util.*;

@Service("mhBannerService")
public class MhBannerServiceImpl extends BaseServiceImpl<MhBannerMapper, MhBanner> implements MhBannerService {


    @Autowired
    private MhModuleService mhModuleService;

    private  LambdaQueryWrapper<MhBanner> commonWrapper(MhBanner mhBanner) {
        LambdaQueryWrapper<MhBanner> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Objects.nonNull(mhBanner.getId()), MhBanner::getId, mhBanner.getId());
        wrapper.eq(Objects.nonNull(mhBanner.getModuleId()), MhBanner::getModuleId, mhBanner.getModuleId());
        wrapper.in(!CollectionUtils.isEmpty(mhBanner.getModuleIdList()), MhBanner::getModuleId, mhBanner.getModuleIdList());
        return wrapper;
    }

    @Override
    public Result<MhBanner> selectOne(MhBanner mhBanner) {
        LambdaQueryWrapper<MhBanner> wrapper = this.commonWrapper(mhBanner);
        return super.selectOne(wrapper);
    }

    @Override
    public Result<List<MhBanner>> selectList(MhBanner mhBanner) {
        LambdaQueryWrapper<MhBanner> wrapper = this.commonWrapper(mhBanner);
        return super.selectList(wrapper);
    }

    @Override
    public PageInfo<MhBanner> selectPage(MhBanner mhBanner) {
        LambdaQueryWrapper<MhBanner> wrapper = this.commonWrapper(mhBanner);
        return super.selectPage(mhBanner, wrapper);
    }

    @Override
    public PageInfo<MhBanner> selectUPage(MhBanner mhBanner) {
        PageInfo<MhBanner> mhBannerPageInfo = this.selectPage(mhBanner);
        List<MhBanner> list = mhBannerPageInfo.getList();
        Set<Long> moduleIdList = new HashSet<>();
        list.forEach(mhBanner1 -> {
            moduleIdList.add(mhBanner1.getModuleId());
        });
        if(!CollectionUtils.isEmpty(moduleIdList)){
            Result<List<MhModule>> listResult = mhModuleService.selectList(new MhModule().setIdList(moduleIdList));
            if(listResult.isSuccess()){
                Map<Long,MhModule> map = new HashMap<>();
                listResult.getData().forEach(mhModule -> {
                    map.put(mhModule.getId(),mhModule);
                });
                list.forEach(mhBanner1 -> {
                    mhBanner1.setMhModule(map.get(mhBanner1.getModuleId()));
                });
            }
        }
        return mhBannerPageInfo;
    }

    @Override
    public Result<Integer> removeById(Serializable id) {
        MhBanner mhBanner = new MhBanner().setStatus(SysConstant.DATA_DELETE_STATUS)
                .setId((Long) id);
        return this.updateById(mhBanner);
    }

}